package com.wyy.filter;

import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Configuration
public class FeignConfig {

    @Bean
    public RequestInterceptor requestInterceptor() {
        return requestTemplate -> {
            //1.RequestContextHolder拿到当前请求的数据，相当与拿到controller入参的HttpServletRequest
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            //老请求
            HttpServletRequest request = requestAttributes.getRequest();

            //2.同步请求头信息->cookie
            String Authorization = request.getHeader("Authorization");
            requestTemplate.header("Authorization", Authorization);
        };
    }
}
